/*
 * @Author: 来自火星的码农 15122322+heyzhi@user.noreply.gitee.com
 * @Date: 2025-10-12 17:22:54
 * @LastEditors: 来自火星的码农 15122322+heyzhi@user.noreply.gitee.com
 * @LastEditTime: 2025-10-13 11:22:22
 * @FilePath: /GateServer/src/validator/registervalidator.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "registervalidator.h"
#include "../util/util.h"
namespace Yizhi {
   

    RegisterValidator::validateResult RegisterValidator::validate(const User&user) {
        std::string value;
        if(!validateVarifycode(user.getEmail(),value)){
            return VARIFY_CODE_INVALID;
        }else{
            if(user.getVarifycode()!=value){
                return VARIFY_CODE_EXPIRED;
            }
        }

        if(checkUserExists(user.getUsername(),user.getEmail())){
            return USER_EXISTS;
        }
        return SUCCESS;
    }

    bool RegisterValidator::validPasswordMatch(const std::string &psw,const std::string &repsw){
        return psw==repsw;
    }
    bool RegisterValidator::validateVarifycode(const std::string &email,std::string &code){
       auto res=RedisMgr::GetInstance()->getvalue(Code_Prefix+email,code);
       return res;
    }
    bool RegisterValidator:: checkUserExists(const std::string& username,const std::string &email){
        auto res=MysqlMgr::GetInstance()->ExistUser(username,email);
        return res;
    }

}